package com.ebank.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Profile extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		PrintWriter out = response.getWriter();
		if(session.getAttribute("username")!=null){
		Statement stat = null;
		Connection con=null;
		ResultSet rs=null;
		PreparedStatement pst = null;
		String selectSQL = "select * from account ";
		String loginName=(String) (session.getAttribute("username"));
		
try {
	Class.forName("com.mysql.jdbc.Driver");
	con = DriverManager.getConnection(
	      "jdbc:mysql://localhost/user?useUnicode=true&characterEncoding=Big5",
	      "root","root");
	 pst=con.prepareStatement("select * from information WHERE username=?");
	 pst.setString(1, loginName);
	 rs=pst.executeQuery();
	 out.print("<table border=1 height=100 width=500 >");
	 out.print("<tr><th>UserName</th><th>Sex</th><th>Address</th><th>E-mail</th><th>Phone</th></tr>");
	 while(rs.next()){
		 out.print("<tr><td>"+rs.getString(1)+
				 "</td><td>"+rs.getString(2)+
				 "</td><td>"+rs.getString(3)+
				 "</td><td>"+rs.getString(4)+
				 "</td><td>"+rs.getInt(5)+
				 "</td></tr>");
	 }
	 out.print("</table>");
	 out.print("<input name=edit type=button id=Submit onClick=javascript:location.href='/EBanking/EditPhoneNumber' value=EditPhoneNumber><br><br>");
	 out.print("<input name=edit type=button id=Submit onClick=javascript:location.href='/EBanking/EditPassword' value=EditPasswrod><br><br>");
	 out.print("<input name=Main type=button id=Main onClick=javascript:location.href='/EBanking/Main' value=Main >");

	
} catch (Exception e) {
	// TODO: handle exception
	}finally{
		if(rs!=null){
			try {
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			rs=null;
		}
		if(pst!=null){
			try {
				pst.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			pst=null;
		}
		if(con!=null){
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			con=null;
		}
	}}else{ request.getRequestDispatcher("/View/Wong.jsp").forward(request, response);
	} 
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
	}

}
